package com.gxa.community.agent.service.agentindexservice.agentindeximpl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gxa.community.agent.service.agentindexservice.AgentIndexService;
import com.gxa.community.agent.utils.UrlUtils;
import com.gxa.community.core.pojo.bo.classification.GoodsInfoBo;
import com.gxa.community.core.pojo.dto.classification.ClassificationDTO;
import com.gxa.community.core.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.util.StringUtils;
import org.springframework.web.client.RestTemplate;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Service
public class AgentIndexServiceImpl implements AgentIndexService {
    @Autowired
    RestTemplate restTemplate;
    @Autowired
    RedisTemplate redisTemplate;
    @Autowired
    private UrlUtils urlUtils;
    @Autowired
    JmsMessagingTemplate jmsMessagingTemplate ;

    private static  final String AGENT_INDEX_INFO="indexinfo:";
    private static  final  String TODAY_SHARE="todayshare:";
    private static  final  String TODAY_SECKILL="todayseckill:";
    private static  final  String TODAY_GROUP="todaygroup:";
    private static  final  String CLASSIFICTION="classification:";
    public static final String GOODS_NAME = "goodsName:";
    @Override
    public R getAgentIndexInfo(Integer userId) {
       //发起远程连接
        String url = "http://localhost:8080/agent/index/infos";
        MultiValueMap<String,Integer> pramas = new LinkedMultiValueMap<>();
        pramas.add("userId",userId);
        ResponseEntity<R> r = restTemplate.postForEntity(url, pramas, R.class);
        Object data = r.getBody().get("data");
        if (data==null)  return R.error(306,"没有该团长信息");
        System.err.println(data);
        //将agent首页信息存入redis
        redisTemplate.opsForValue().set(AGENT_INDEX_INFO+userId,data);
        return R.ok().put("data",data);
    }

    @Override
    public R createUrl(Integer userId) {
        String url = urlUtils.agentUrl()+userId;//生成链接
        //存入数据库
        jmsMessagingTemplate.convertAndSend("shareurl",url);
        return R.ok().put("data",url);
    }

    //查询好货上新标签下的商品
    @Override
    public R Todayshare() {
        //先去redis查询
        Object o = redisTemplate.opsForValue().get(TODAY_SECKILL);
        System.out.println(o);
        if (null!=o&&""!=o) return R.ok().put("data",o);
        System.err.println("从后台数据库中查询的...");
        //向后台发送数据查询请求
        String url = "http://localhost:8080/agent/index/wellgoods";
//        MultiValueMap<String,Integer> pramas = new LinkedMultiValueMap<>();
//        pramas.add("userId",userId);
        ResponseEntity<R> r = restTemplate.postForEntity(url, null, R.class);
        Object data = r.getBody().get("data");
        System.err.println(data);
        //将agent首页信息存入redis
        redisTemplate.opsForValue().set(TODAY_SHARE,data);
        return R.ok().put("data",data);
    }

    @Override
    public R todaySeckill() {
        //先去redis查询
        Object seckill = redisTemplate.opsForValue().get(TODAY_SECKILL);
        System.out.println(seckill);
        if (null!=seckill&&""!=seckill) return R.ok().put("data",seckill);
        System.err.println("从后台数据库中查询的...");
        //redis没有就向后台发送数据查询请求
        String url = "http://localhost:8080/agent/index/seckillgoods";
        ResponseEntity<R> r = restTemplate.postForEntity(url, null, R.class);
        Object data = r.getBody().get("data");
        System.err.println(data);
//        if (data==null||data=="") return R.error(306,"没有该团长信息");
        //将agent首页信息存入redis
        redisTemplate.opsForValue().set(TODAY_SECKILL,data);
        return R.ok().put("data",data);
    }

    @Override
    public R todayGroup() {
        //先去redis查询
        Object seckill = redisTemplate.opsForValue().get(TODAY_GROUP);
        System.out.println(seckill);
        if (null!=seckill&&""!=seckill) return R.ok().put("data",seckill);
        System.err.println("从后台数据库中查询的...");
        //redis没有就向后台发送数据查询请求
        String url = "http://localhost:8080/agent/index/groupgoods";
        ResponseEntity<R> r = restTemplate.postForEntity(url, null, R.class);
        Object data = r.getBody().get("data");
        System.err.println(data);
        //将agent首页信息存入redis
        redisTemplate.opsForValue().set(TODAY_GROUP,data);
        return R.ok().put("data",data);
    }

    @Override
    public R classification() {
        //先去redis查询
        Object classification = redisTemplate.opsForValue().get(CLASSIFICTION);
        System.out.println(classification);
        if (null!=classification&&""!=classification) return R.ok().put("data",classification);
        System.err.println("从后台数据库中查询的...");
        //redis没有就向后台发送数据查询请求
        String url = "http://localhost:8081/classification/queryallclassificationcontaingoodsinfo";
        ResponseEntity<Object> forEntity = restTemplate.getForEntity(url, null, R.class);
        System.err.println(forEntity);
        Object data = redisTemplate.opsForValue().get(CLASSIFICTION);
        return R.ok().put("data",data);
    }

    @Override
    public R serach(String name) {
        //先去redis查询
        Object serach = redisTemplate.opsForValue().get(GOODS_NAME+name);
        System.out.println(serach);
        if (null!=serach&&""!=serach) return R.ok().put("data",serach);
        System.err.println("从后台数据库中查询的...");
        Map<String,String> map = new HashMap<>();
        map.put("name",name);
        //redis没有就向后台发送数据查询请求
        String url = "http://localhost:8081//classification/searchgoods?name={name}";
        String forObject = restTemplate.getForObject(url, String.class, map);
        System.err.println(forObject);
        Object data = redisTemplate.opsForValue().get(GOODS_NAME+name);
        return R.ok().put("data",data);
    }
}
